import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Color;

public class vTablero extends JFrame implements ActionListener {
    private JPanel panelPpal = new JPanel ();
    private JPanel tablero = new JPanel ();
    private JPanel menu = new JPanel ();
    private JLabel info = new JLabel (" Introduce los datos y pulsa calcular.");
    private JButton fTablero[][] = new JButton [8][8];
    private JLabel lFilas = new JLabel("Filas: ");
    private JTextField tFilas = new JTextField("0", 4);
    private JLabel lColumnas = new JLabel("Columnas: ");
    private JTextField tColumnas = new JTextField("0", 4);
    private JButton bCalcular = new JButton("Calcular");

    public vTablero () {
        int i = 0;
        int j = 0;
        
        setTitle("Salto del caballo");
        setSize(540,600);
        
        panelPpal.setLayout (new BorderLayout ());
        tablero.setLayout (new GridLayout (8,8));
        
        add (menu, BorderLayout.NORTH);
        add (tablero, BorderLayout.CENTER);
        add (info, BorderLayout.SOUTH);
        
        menu.add (lFilas);
        menu.add (tFilas);
        menu.add (lColumnas);
        menu.add (tColumnas);
        menu.add (bCalcular);
        
        bCalcular.addActionListener (this);
        tFilas.addActionListener (this);
        tColumnas.addActionListener (this);
        
        tablero.setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 4, 4, 4));
        
        for (i = 0; i < 8; i++) {
            for (j = 0; j < 8; j++) {
                fTablero[i][j] = new JButton ("-");
                tablero.add (fTablero[i][j]);
                if ((i % 2 == 0 && j % 2 == 1) || (i % 2 == 1 && j % 2 == 0)) {
                    fTablero[i][j].setBackground(Color.black);
                }
                else {
                    fTablero[i][j].setBackground(Color.white);
                }
            }
        }
    }
    
    public void actionPerformed(ActionEvent evento) {
        Object source = evento.getSource();
        int valor = 0;
        int i = 0;
        int j = 0;
        
        if (source == tFilas) {
            valor = Integer.parseInt(tFilas.getText());
            if (valor > 8 || valor < 0) {
                tFilas.setText("0");
                info.setText (" Valor en las filas incorrecto.");
            } else {
                info.setText (" Introduce los datos y pulsa calcular.");
            }
        }
        
        if (source == tColumnas) {
            valor = Integer.parseInt(tColumnas.getText());
            if (valor > 8 || valor < 0) {
                tColumnas.setText("0");
                info.setText (" Valor en las columnas incorrecto.");
            } else {
                info.setText (" Introduce los datos y pulsa calcular.");
            }
        }
        
        if (source == bCalcular) {
            valor = Integer.parseInt(tColumnas.getText());
            if (valor > 8 || valor < 0) {
                tColumnas.setText("0");
                info.setText (" Valor en las columnas incorrecto.");
            } else {
                valor = Integer.parseInt(tFilas.getText());
                if (valor > 8 || valor < 0) {
                    tFilas.setText("0");
                    info.setText (" Valor en las filas incorrecto.");
                } else {
                    Caballo c = new Caballo ();
                    c.procesar(Integer.parseInt(tFilas.getText()), Integer.parseInt(tColumnas.getText()));
                    for (i = 0; i < 8; i++) {
                        for (j = 0; j < 8; j++) {
                            fTablero[i][j].setText(c.getNum(i, j) + "");
                        }
                    }
                }
            }
        }
    }
}